package com.education.welco.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.core.BaseController;
import com.education.common.pojo.LCatalog;
import com.education.common.pojo.LCatalogMenu;
import com.education.common.utils.ApiReturnObject;
import com.education.welco.pojo.LCatalogLabel;
import com.education.welco.pojo.LResources;
import com.education.welco.service.ILCatalogLabelService;
import com.education.welco.service.ILCatalogMenuService;
import com.education.welco.service.ILCatalogService;
import com.education.welco.service.ILResourcesService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 备课目录 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@RestController
@RequestMapping("/lCatalogMenu")
public class LCatalogMenuController extends BaseController {

    @Autowired
    private ILCatalogMenuService catalogMenuService;
    @Autowired
    private ILCatalogLabelService catalogLabelService;
    @Autowired
    private ILResourcesService resourcesService;
    @Autowired
    private ILCatalogService lCatalogService;


    @ApiOperation(value = "查询用户协议", httpMethod = "GET")
    @GetMapping("/selectMenuList")
    public ApiReturnObject selectMenuList(Integer parentId){
        List<LCatalogMenu> menus = catalogMenuService.findByParentId(parentId);
         return new ApiReturnObject("200", "success", menus);
    }

    /**
     *  查看备课目录列表
     */
    @GetMapping("/list")
    public ApiReturnObject list(String name,Integer pageNum,Integer pageSize){
        LCatalog lCatalogMenu = new LCatalog();
        if( name != null && !"".equals(name)){
            lCatalogMenu.setCatalogName(name);
        }
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        List<LCatalog> lCatalogs = lCatalogService.selectLCatalogMenuListOther2(lCatalogMenu);
//        List<LCatalogMenu> lCatalogMenus = catalogMenuService.selectLCatalogMenuListOther2(lCatalogMenu);
       /* //去掉重复数据    新建一个集合 往里面存储逐渐id 如已存在，删除掉原来集合中的数据
        List<Integer> list = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for (int i = 0; i < lCatalogMenus.size(); i++) {
            if(list.contains(lCatalogMenus.get(i).getId())){
                //包含 删除
                list2.add(i);
            }else {
                list.add(lCatalogMenus.get(i).getId());
            }
        }
        for (int i = 0; i < list2.size(); i++) {
            lCatalogMenus.remove(list2.get(i) - i);
        }*/
        PageInfo<LCatalog> pageInfo = new PageInfo<>(lCatalogs);
        return new ApiReturnObject("200","查询成功",pageInfo);
    }

    //查询目录列表
    public List<LCatalogMenu> listSon(List<LCatalogMenu> lList){
        for (LCatalogMenu catalogMenu : lList) {
            //根据第一级的id查询出下级列表
            List<LCatalogMenu> jrCatalogs = catalogMenuService.findByParentId(catalogMenu.getId());
            if(jrCatalogs != null && jrCatalogs.size() > 0){
                for (LCatalogMenu jrCatalog : jrCatalogs) {
                    jrCatalog.setLabel(jrCatalog.getName());
                }
                //设置子级集合
                catalogMenu.setChildren(listSon(jrCatalogs));
            }
        }
        return lList;
    }

    /**
     *  添加时课程目录
     */
    @GetMapping("/listCatalog")
    public ApiReturnObject listCatalog(){
        LCatalogMenu catalogMenu = new LCatalogMenu();
        catalogMenu.setParentId(0);
        //查出第一级列表
        List<LCatalogMenu> lCatalogMenus = catalogMenuService.selectLCatalogMenuList(catalogMenu);
        for (LCatalogMenu lCatalogMenu : lCatalogMenus) {
            lCatalogMenu.setLabel(lCatalogMenu.getName());
        }
        //目录列表
        List<LCatalogMenu> lCatalogMenuList = listSon(lCatalogMenus);
        return new ApiReturnObject("200","查询成功",lCatalogMenuList);
    }

    /**
     *  查询教学云课堂详情
     */
    @GetMapping("/selectOne")
    public ApiReturnObject selectOne(Integer id){
        if(id != null){
            LCatalogMenu catalogMenu = catalogMenuService.selectLCatalogMenuById(id);
            return new ApiReturnObject("200","查询成功",catalogMenu);
        }
        return new ApiReturnObject("201","缺少参数","");
    }




    /**
     *  添加平台课程
     */
    @PostMapping("/add")
    private ApiReturnObject addSave(@RequestBody LCatalogMenu catalogMenu){
        //创建时间
        catalogMenu.setCreateTime(new Date());
        //父级id
//        catalogMenu.setParentId(0);
        //学习人数
        catalogMenu.setStudysum("0");
        //是否删除
        catalogMenu.setDelFlag("0");
        // 分类 1理论课堂 2实训课堂
        catalogMenu.setCatalogState(2);
        catalogMenuService.insertLCatalogMenu(catalogMenu);
        if(catalogMenu.getLabelList() != null && catalogMenu.getLabelList().size() > 0 ){
            //获取自动生成的主键 id
            Integer id = catalogMenu.getId();
            //往标签中间表中添加数据
            LCatalogLabel label = new LCatalogLabel();
            label.setCatalogId(id);
            for (Integer integer : catalogMenu.getLabelList()) {
                label.setLabelId(integer);
                catalogLabelService.insertLCatalogLabel(label);
            }
        }
        return new ApiReturnObject("200","添加成功","");
    }

    /**
     *  修改平台课程
     */
    @PostMapping("/update")
    public ApiReturnObject update(@RequestBody LCatalogMenu catalogMenu){
        if(catalogMenu.getId() != null){
            catalogMenu.setStudysum("12");
            catalogMenuService.updateLCatalogMenu(catalogMenu);
        }
        return new ApiReturnObject("201","缺少id参数","");
    }

    /**
     *  删除平台课程
     */
    @GetMapping("/del")
    public ApiReturnObject del(Integer id){
        if(id != null){
            //如果下面存在子级目录 或者 存在资源 删除失败
            List<LCatalogMenu> catalogMenuList = catalogMenuService.findByParentId(id);
            if(catalogMenuList != null && catalogMenuList.size() > 0){
                return new ApiReturnObject("201","该目录存在子目录,请先删除子目录","");
            }
            LResources resources = new LResources();
            resources.setRCatalogid(id);
            List<LResources> sResources = resourcesService.selectLResourcesList(resources);
            if(sResources != null && sResources.size() > 0){
                return new ApiReturnObject("201","该目录下面存在资源，请先删除资源","");
            }
            //删除的是第一级或者第二级的话 删除目录以及下面的各种资源
            catalogMenuService.deleteLCatalogMenuById(id);
            return new ApiReturnObject("200","删除成功","");
        }
        return new ApiReturnObject("201","缺少相关参数","");
    }

}